//
//  HOTMemoRangeViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-22.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTJobTypeChooseViewController.h"
#import "HOTCommChooseCell.h"

@interface HOTJobTypeChooseViewController () {
    
    NSDictionary *_choosedInfo;
    NSIndexPath *_chooseIndexPath;
}

@end

@implementation HOTJobTypeChooseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil choosedInfo:(NSDictionary *)chooseInfo
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _choosedInfo = chooseInfo;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //[self setNaviTitle:@"血液型"];
    
    //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_profile"]];
    
    [self addRightBtnTarget:self action:@selector(descideBtnClicked) image:[UIImage imageNamed:@"btn_decision_off"] hImage:[UIImage imageNamed:@"btn_decision_on"]];
    
    //_dataList = @[@{@"id":@"1", @"name":@"A型"},@{@"id":@"2", @"name":@"B型"},
    //              @{@"id":@"3", @"name":@"O型"},@{@"id":@"4", @"name":@"AB型"}];
}

#pragma mark - UItableView DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTCommChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTCommChooseCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.indexPath = indexPath;
    NSDictionary *dic = [_dataList objectAtIndex:indexPath.row];
    NSString *id_ = [dic objectForKey:@"id"];
    NSString *name = [dic objectForKey:@"name"];

    cell.cateTitleLabel.text = name;
    
    if (_chooseIndexPath) {
        if (_chooseIndexPath.row == indexPath.row) {
            cell.radioBtn.selected = YES;
        } else {
            cell.radioBtn.selected = NO;
        }
        
    } else {
        
        if ([[_choosedInfo objectForKey:@"id"] isEqualToString:id_]) {
            _chooseIndexPath = indexPath;
            cell.radioBtn.selected = YES;
        } else {
            cell.radioBtn.selected = NO;
        }
    }
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 62;
}

#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HOTCommChooseCell *cell;
    if (_chooseIndexPath) {
        
        HOTCommChooseCell *cell = (HOTCommChooseCell *)[tableView cellForRowAtIndexPath:_chooseIndexPath];
        cell.radioBtn.selected = NO;
    }
    
    _chooseIndexPath = indexPath;
    cell = (HOTCommChooseCell *)[tableView cellForRowAtIndexPath:_chooseIndexPath];
    cell.radioBtn.selected = YES;
}


#pragma mark 私有函数
-(void)descideBtnClicked {
    
    if (self.choosedDataBlock) {
        
        NSDictionary *dic = [self.dataList objectAtIndex:_chooseIndexPath.row];
        self.choosedDataBlock(dic);
    }

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
